package controler;

import java.awt.event.KeyEvent;

import userInterface.BrickBreakerPanel;
import userInterface.MainWindow;
import baseObject.IBrickBreaker;
import baseObject.IBrickBreaker.TFinished;
import baseObject.IBrickBreaker.TMove;

public class Control implements IControler, Runnable{
	private IBrickBreaker brickBreackerModel;
	private MainWindow mainWindowView;
	
	public Control(IBrickBreaker brickBreackerModel, MainWindow mainWindowView){

		this.brickBreackerModel = brickBreackerModel;
		this.mainWindowView = mainWindowView;
		this.mainWindowView.setControler(this);
		this.mainWindowView.initComponent();
		BrickBreakerPanel panel = this.mainWindowView.getPanel();
		this.brickBreackerModel.addIBrickBreakerObserver(panel);
		this.brickBreackerModel.initLevelOne();
	}

	public IBrickBreaker getBrickBreackerModel() {
		return brickBreackerModel;
	}

	public MainWindow getMainWindowView() {
		return mainWindowView;
	}
	
	public void keyPressed(KeyEvent e) {
		switch(e.getKeyCode()) { 
		case KeyEvent.VK_LEFT:
			this.brickBreackerModel.setPlayerDirection(TMove.LEFT);
			break;
		case KeyEvent.VK_RIGHT :
			this.brickBreackerModel.setPlayerDirection(TMove.RIGHT);
			break;
		case KeyEvent.VK_SPACE:
			if(mainWindowView.getPanel().gameState() == TFinished.GAMEOVER){
				System.out.println("here");
			}
		}

	}

	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub

	}
	
	public void run(){
		while(true){
			this.brickBreackerModel.calculateStep();
			try {
				Thread.sleep(5);
			} catch (InterruptedException e) {
				System.err.println(e);
			}
		}
	}
}
